package com.abe.mvp.view.activity;

import java.util.List;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.abe.config.Constants;
import com.abe.config.SettingParam;
import com.abe.mvp.contact.WebServerContact;
import com.abe.mvp.model.bean.MenuBean;
import com.abe.mvp.model.bean.SettingBean;
import com.abe.mvp.model.bean.http.VersionBean;
import com.abe.mvp.presenter.HttpVersionPresenter;
import com.abe.mvp.view.activity.FragmentCallbackListener.SettingCallbackListener;
import com.abe.mvp.view.activity.base.BaseFragmentActivity;
import com.abe.mvp.view.adapter.SettingMenuAdapter;
import com.abe.mvp.view.fragment.SettingDeviceFragment;
import com.abe.mvp.view.fragment.SettingLanguageFragment;
import com.abe.mvp.view.fragment.SettingLoginTypeFragment;
import com.abe.mvp.view.fragment.SettingPassFragment;
import com.abe.mvp.view.fragment.SettingTiyanFragment;
import com.abe.mvp.view.fragment.SettingWifiFragment;
import com.abe.mvp.view.fragment.base.BaseSettingFragment;
import com.abe.threadmill.R;
import com.abe.utils.AppUtils;
import com.abe.utils.FileDownLoadUtil;
import com.abe.utils.FileDownloadListener;
import com.abe.utils.NormalUtils;
import com.abe.view.CommonProgressDialog;
import com.squareup.picasso.Picasso;

@ContentView(R.layout.activity_tm_setting)
public class SettingActivity extends BaseFragmentActivity implements
		WebServerContact.VersionUpdateView, FileDownloadListener,
		OnItemClickListener, SettingCallbackListener {

	@SuppressWarnings("unused")
	private WebServerContact.VersionPresenter versionPresenter;
	/** 自定义下载进度对话框 */
	private CommonProgressDialog mDialog = null;
	/** 软件版本更新信息获取状态 */
	private boolean version_status = false;

	@ViewInject(R.id.show_exercise_button_back)
	private LinearLayout show_exercise_button_back;
	@ViewInject(R.id.show_exercise_text_title)
	private TextView show_exercise_text_title;
	@ViewInject(R.id.show_exercise_right)
	private LinearLayout show_exercise_right;

	@ViewInject(R.id.tabmenu_layout)
	private ImageView tabmenu_layout;
	/********************************/
	@ViewInject(R.id.setting_main_layout)
	private LinearLayout setting_main_layout;

	@ViewInject(R.id.setting_list_menu)
	private ListView setting_list_menu;
	private List<MenuBean> menuBeans;
	private SettingMenuAdapter adapter;

	@ViewInject(R.id.setting_layout_content)
	private FrameLayout setting_layout_content;
	private BaseSettingFragment current_fragment;

	/********************************/
	@ViewInject(R.id.setting_pass_layout)
	private View setting_pass_layout;

	@ViewInject(R.id.set_pass_edit_pass)
	private EditText set_pass_edit_pass;
	@ViewInject(R.id.set_pass_button_submit)
	private Button set_pass_button_submit;

	private boolean isChangeLanguage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initBackground();
		initView();
	}

	private void initBackground() {
		/** 菜单页背景 */
		Picasso.with(this).load(R.drawable.run_image_bg_05).centerCrop()
				.placeholder(R.drawable.app_theme_bg).resize(1280, 720)
				.into(tabmenu_layout);
	}

	private void initView() {
		isChangeLanguage = false;
		setting_main_layout.setVisibility(View.GONE);
		setting_pass_layout.setVisibility(View.VISIBLE);
		show_exercise_text_title.setText(R.string.common_setting);
		show_exercise_button_back.setVisibility(View.VISIBLE);
		show_exercise_right.setVisibility(View.INVISIBLE);
		/** 内容 */
		versionPresenter = new HttpVersionPresenter(this);
		menuBeans = SettingParam.getSettingMenuBeans(0);
		adapter = new SettingMenuAdapter(this, menuBeans, scale,
				SettingMenuAdapter.TYPE_SETTINGMENU);
		setting_list_menu.setAdapter(adapter);
		setting_list_menu.setOnItemClickListener(this);
		initFragment();
	}

	/** 添加子页面 */
	private void initFragment() {
		switchFragment(0);
	}

	@Event(value = R.id.set_pass_button_submit, type = View.OnClickListener.class)
	private void checkPass(View view) {
		String pass = set_pass_edit_pass.getText().toString().trim();
		if (!pass.equals("")) {
			SettingBean settingBean = SettingParam.getSettingBean();
			if (pass.equals(settingBean.getSetPassword())) {
				setting_main_layout.setVisibility(View.VISIBLE);
				setting_pass_layout.setVisibility(View.GONE);
				show_exercise_right.setVisibility(View.VISIBLE);
			} else {
				showToast(R.string.common_pass_error);
			}
		} else {
			showToast(R.string.common_pass_empty);
		}
	}

	@Event(value = R.id.show_exercise_button_back, type = View.OnClickListener.class)
	private void buttonBack(View view) {
		if (isChangeLanguage) {
			NormalUtils.setAppLanguage();
			Intent intent = new Intent();
			intent.putExtra("extra", Constants.SettingLanguage);
			setResult(RESULT_OK, intent);
		}
		back();
	}

	@Event(value = R.id.show_exercise_right, type = View.OnClickListener.class)
	private void home(View view) {
		gotoActivity(AppsActivity.class);
	}

	@Override
	public void updateLanguage() {
		isChangeLanguage = true;
	}

	@Override
	public void onGetVersionInfoSuccess(VersionBean bean) {
		if (!bean.getVersion().equals("")) {
			version_status = AppUtils.isNeedUpdate(bean.getVersion());
			if (version_status) {
				/** 需要更新，下载新版本APP并安装 */
				FileDownLoadUtil util = new FileDownLoadUtil();
				util.setFileDownloadListener(this);
				util.down(bean.getUrl(), Constants.getUpdateAPP()
						.getAbsolutePath());
				/** 下载进度 */
				mDialog = new CommonProgressDialog(this);
				mDialog.setMessage(NormalUtils
						.getString(R.string.common_downloading));
				mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				mDialog.setOnCancelListener(new OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialog) {
						dialog.dismiss();
					}
				});
				mDialog.show();
			} else {
				showToast(R.string.common_version_isnewest);
			}
		} else {
			showToast(R.string.common_check_update_error);
		}
	}

	@Override
	public void onFinish(int type) {
	}

	@Override
	public void onDownLoadSuccess() {
		/** 消除对话框 */
		mDialog.cancel();
		/** 下载完成，安装APP */
		AppUtils.installNewApk(getApplicationContext(), Constants
				.getUpdateAPP().getAbsolutePath());
	}

	@Override
	public void onRefreshProgress(int progress, long len, long length) {
		/** 刷新进度条 */
		mDialog.setMax((int) length);
		mDialog.setProgress((int) len);
	}

	@Override
	public void onRequestFinish() {

	}

	@Override
	public void onRequestFail(String errorMsg) {
		showToast(errorMsg);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
		/** 菜单变化 */
		menuBeans = SettingParam.getSettingMenuBeans(pos);
		adapter.setListData(menuBeans);
		setting_list_menu.setAdapter(adapter);
		/** 内容变化 */
		switchFragment(pos);
	}

	private void switchFragment(int pos) {
		FragmentManager fm;
		FragmentTransaction transaction;
		fm = getSupportFragmentManager();
		transaction = fm.beginTransaction();
		switch (pos) {
		case 0:
			current_fragment = new SettingPassFragment();
			break;
		case 1:
			current_fragment = new SettingLoginTypeFragment();
			break;
		case 2:
			current_fragment = new SettingDeviceFragment();
			break;
		case 3:
			current_fragment = new SettingLanguageFragment();
			break;
		case 4:
			current_fragment = new SettingWifiFragment();
			break;
		case 5:
			current_fragment = new SettingTiyanFragment();
			break;
		}
		transaction.replace(R.id.setting_layout_content, current_fragment);
		transaction.commit();
	}

}
